package nju.eai.seller.server;

import java.util.MissingResourceException;

import nju.eai.corba.paymentSystem.SystemC;
import nju.eai.corba.paymentSystem.SystemCHelper;
import nju.eai.corba.sellerSystem.SystemB;

import org.omg.CORBA.ORB;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.PortableServer.POA;

public class TestServer {

		ORB orb;
		POA rootPOA;
		NamingContextExt ncRef;
		SystemB sb;
		SystemC sc;

		/**
		 * @param name
		 *            the name of CORBA object
		 * @return -return the CORBA Object
		 */
		public SystemC init(String name) {
			String initArgs[] = new String[4];
			try {
				initArgs[0] = "-ORBInitialPort";
				initArgs[1] = "900";
				initArgs[2] = "-ORBInitialHost";
				initArgs[3] = "Death-NJU";
				// create an ORB instance,telling it the Name Service's location
				this.orb = ORB.init(initArgs, null);

				// get the root naming context
				org.omg.CORBA.Object objRef = orb
						.resolve_initial_references("NameService");
				// Use NamingContextExt instead of NamingContext. This is
				// part of the Interoperable naming Service.
				ncRef = NamingContextExtHelper.narrow(objRef);

				// resolve the Object Reference in Naming
				this.sc = SystemCHelper.narrow(ncRef.resolve_str(name));
				System.out.println("Obtained a handle of CalculateService OBJ.");
				return this.sc;
			} catch (MissingResourceException e) {
				e.printStackTrace();
				return null;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}

		public static void main(String[] args) {
			TestServer ts =  new TestServer();
			SystemC c =  ts.init("Payment_Server");

			if (c != null) {
				String user = "tom";
				String password = "tom";
				
				System.out.println(c.balance(user, password));
				System.out.println(c.deal("tom", "tom", "jerry", "21312", 1000));
				
				System.out.println(c.balance(user, password));
				c.fail("21312");
				System.out.println(c.balance(user, password));
				
				
				
				
			}
			
			

		}
	


}
